package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.net.PostGongGao;
import longcaisuyun.longcai.com.net.PostGongGaoDele;
import longcaisuyun.longcai.com.suyunbean.GonggaoBean;



public class GonggaoActivity extends Activity implements View.OnClickListener {
    private Dialog dialog;
    LinearLayout fanhui_lin;
    PullToRefreshListView listView;
    List<GonggaoBean> list = new ArrayList<GonggaoBean>();
    GongGaoAdapter g = new GongGaoAdapter(GonggaoActivity.this, list);
    String ispage = "";
    String nextpage = "";
    String startpage = "1";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gonggao);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        DataInit();
        ViewInit();
        HttpPost(startpage);
    }

    private void HttpPost(String page) {
        MyApplication.myviewutil.startProgressDialog(GonggaoActivity.this);
        new PostGongGao(MyApplication.myPreferences.readUid(), page, new AsyCallBack<PostGongGao.Info>() {

            @Override
            public void onSuccess(String toast, int type, PostGongGao.Info info) throws Exception {
                super.onSuccess(toast, type, info);

                if (info.message.equals("1")) {
                    ispage = info.ispage;
                    nextpage = info.nextpage;
                    addlist(info.jsonArray);
                    g.notifyDataSetChanged();
                } else {
                    Toast.makeText(GonggaoActivity.this, "获取数据失败，请稍后尝试", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                Toast.makeText(GonggaoActivity.this, "获取数据失败，请检查网络设置后重新尝试", Toast.LENGTH_SHORT).show();


            }
        }).execute(GonggaoActivity.this);
        MyApplication.myviewutil.stopProgressDialog();
        listView.onPullDownRefreshComplete();
        listView.onPullUpRefreshComplete();
    }

    private void DataInit() {


    }

    private void ViewInit() {
        fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        listView = (PullToRefreshListView) findViewById(R.id.listView3);
        listView.setScrollLoadEnabled(true);
        listView.setPullLoadEnabled(false);
        ListView pulllist = listView.getRefreshableView();
        pulllist.setDivider(null);
        pulllist.setAdapter(g);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                HttpPost(startpage);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (ispage.equals("1")) {
                    HttpPost(nextpage);
                } else {
                    Toast.makeText(GonggaoActivity.this, "已加载完毕", Toast.LENGTH_SHORT).show();
                    MyApplication.myviewutil.stopProgressDialog();
                    listView.onPullDownRefreshComplete();
                    listView.onPullUpRefreshComplete();
                }
            }
        });

        pulllist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent();

                intent.putExtra("gonggaoxiangqingid", list.get(position).getId());

                System.out.println("**********" + list.get(position).getId());

                intent.setClass(GonggaoActivity.this, GongGaoXiangQingActivity.class);
                startActivity(intent);
            }
        });

        /*ulllist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                diag(position);
                return false;
            }
        });*/
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui_lin:
                GonggaoActivity.this.finish();
                break;
        }
    }

    public void addlist(JSONArray jsonArray) {
        list.clear();
        if (jsonArray.length() > 0) {
            for (int j = 0; j < jsonArray.length(); j++) {
                GonggaoBean gonggaoBean = new GonggaoBean();
                try {
                    gonggaoBean.setTitle(jsonArray.getJSONObject(j).getString("title"));
                    gonggaoBean.setId(jsonArray.getJSONObject(j).getString("id"));
                    gonggaoBean.setPosttime(jsonArray.getJSONObject(j).getString("posttime"));

                    list.add(gonggaoBean);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        } else {
            Toast.makeText(GonggaoActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
        }
    }

    public void diag(final int j) {
        LayoutInflater inflater = LayoutInflater.from(GonggaoActivity.this);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.item_caozuo_phone, null);
        TextView tv_title = (TextView) layout.findViewById(R.id.textView28);
        tv_title.setText("是否删除该条公告");
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog = new Dialog(GonggaoActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new BitmapDrawable());
        dialog.setCancelable(false);
        dialog.show();
        dialog.setContentView(layout);

        TextView tv_phone = (TextView) layout.findViewById(R.id.t_phone);
        tv_phone.setText("删除");
        TextView tv_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        tv_phone.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                new PostGongGaoDele(MyApplication.myPreferences.readUid(), list.get(j).getId(), new AsyCallBack<PostGongGaoDele.Info>() {
                    @Override
                    public void onSuccess(String toast, int type, PostGongGaoDele.Info info) throws Exception {
                        super.onSuccess(toast, type, info);
                        if (info.message.equals("0")) {
                            Toast.makeText(GonggaoActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(GonggaoActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                            list.remove(j);
                            g.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                    }
                }).execute(GonggaoActivity.this);
                dialog.dismiss();
            }
        });
        tv_cancle.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                dialog.dismiss();

            }
        });
    }
}
